const common = require('../../utils/common')
const { getOrderBInfo, confirmReturn, confirmWarehouse } = require('../../services/order')
Page({
  data: {
    orderData: {
      detail: []
    },
    orderId: '',
    deliveryCode: '',
    confirmText: '',
  },
  onLoad: function ({ id }) {
    if (!id) return
    this.setData({
      orderId: id,
    }, this.getDetails)
  },
  getDetails: function () {
    getOrderBInfo({ orderId: this.data.orderId }).then((res) => {
      this.setData({
        orderData: res.data
      }, this.setConfirmText)
    }).catch(err => {
      common.log(err)
    })
  },
  setConfirmText: function () {
    const { bstatus } = this.data.orderData
    let confirmText = ''
    if (bstatus == 3) {
      // 待提货
      confirmText = '订单交货'
    } else if (bstatus == 11) {
      // 待退回
      confirmText = '确认退回自提点'
    } else if (bstatus == 12) {
      // 待返仓
      confirmText = '确认返仓'
    } else {
      confirmText = ''
    }
    this.setData({
      confirmText
    })
  },
  onConfirm: function () {
    const { orderId } = this.data
    const { bstatus, shopId } = this.data.orderData
    // 待提货
    if (bstatus == 3) {
      wx.navigateTo({ url: `/pages/pick-up-order/index?id=${this.data.orderId}` })
    } else if (bstatus == 11) {
      // 待退回
      confirmReturn({ orderId, afterId: shopId }).then((res) => {
        common.showToast(res.message)
      }).catch(err => {
        common.log(err)
      })
    } else if (bstatus == 12) {
      // 待返仓
      confirmWarehouse({ orderId, afterId: shopId }).then((res) => {
        common.showToast(res.message)
      }).catch(err => {
        common.log(err)
      })
    }
  },
  makePhoneCall: function (e) {
    const { phone } = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  }
})
